const crypto = require('crypto');
const ResponseUtil = require('../utils/ResponseUtil');
const VRCE = require('../utils/ViewResultCodeEnum');
const RCEM = require('../utils/ResultCodeEnumMsg');
const Constant = require('../config/Constant');
const OperatorType = require('../config/OperatorType');
const ROLES = [OperatorType.BROKER, OperatorType.LAWYER, OperatorType.FUNDRAISER];
const globToRegExp = require('glob-to-regexp');

module.exports = (options, app) => {
    return async function middlewarreone(ctx, next) {

        const prefix = '/pet/admin';


        const GET = 'GET-';
        const DELETE = 'DELETE-';
        const PUT = 'PUT-';
        const POST = 'POST-';

        const noPermissionList = [];

        //需要验证的权限
        const permissionMap = {
            [`${GET}${prefix}/menu/list`]: 'system:menu:list',
            [`${DELETE}${prefix}/menu/list/*`]: 'system:menu:remove',
            [`${PUT}${prefix}/menu`]: 'system:menu:edit',
            [`${POST}${prefix}/menu`]: 'system:menu:add',
            [`${GET}${prefix}/menu/*`]: 'system:menu:query',

            [`${GET}${prefix}/sysuser/list`]: 'system:admin:list',
            [`${DELETE}${prefix}/sysuser/*`]: 'system:admin:remove',
            [`${POST}${prefix}/sysuser`]: 'system:admin:add',
            [`${PUT}${prefix}/sysuser`]: 'system:admin:edit',
            [`${PUT}${prefix}/sysuser/resetPwd`]: 'system:admin:resetPwd',
            [`${GET}${prefix}/sysuser/*`]: 'system:admin:query',

            [`${GET}${prefix}/role/list`]: 'system:role:list',
            [`${DELETE}${prefix}/role/*`]: 'system:role:remove',
            [`${POST}${prefix}/role`]: 'system:role:add',
            [`${PUT}${prefix}/role`]: 'system:role:edit',
            [`${PUT}${prefix}/role/perms`]: 'system:role:edit',
            [`${GET}${prefix}/role/*`]: 'system:role:query',

            [`${GET}${prefix}/shop/goods/category/list`]: 'system:shop:goods:category:list',
            [`${GET}${prefix}/shop/goods/category/*`]: 'system:shop:goods:category:detail',
            [`${POST}${prefix}/shop/goods/category`]: 'system:shop:goods:category:add',
            [`${PUT}${prefix}/shop/goods/category`]: 'system:shop:goods:category:edit',
            [`${DELETE}${prefix}/shop/goods/category/*`]: 'system:shop:goods:category:delete',
            [`${POST}${prefix}/shop/goods/category/tag`]: 'system:shop:goods:category:tag:add',
            [`${PUT}${prefix}/shop/goods/category/tag`]: 'system:shop:goods:category:tag:edit',
            [`${DELETE}${prefix}/shop/goods/category/tag/*`]: 'system:shop:goods:category:tag:delete',

            [`${GET}${prefix}/mall/integral/category/list`]: 'system:mall:integral:category:list',
            [`${GET}${prefix}/mall/integral/category/*`]: 'system:mall:integral:category:detail',
            [`${POST}${prefix}/mall/integral/category`]: 'system:mall:integral:category:add',
            [`${PUT}${prefix}/mall/integral/category`]: 'system:mall:integral:category:edit',
            [`${DELETE}${prefix}/mall/integral/category/*`]: 'system:mall:integral:category:delete',
            [`${POST}${prefix}/mall/integral/category/tag`]: 'system:mall:integral:category:tag:add',
            [`${PUT}${prefix}/mall/integral/category/tag`]: 'system:mall:integral:category:tag:edit',
            [`${DELETE}${prefix}/mall/integral/category/tag/*`]: 'system:mall:integral:category:tag:delete',

            [`${GET}${prefix}/mall/goods/category/list`]: 'system:mall:goods:category:list',
            [`${GET}${prefix}/mall/goods/category/*`]: 'system:mall:goods:category:detail',
            [`${POST}${prefix}/mall/goods/category`]: 'system:mall:goods:category:add',
            [`${PUT}${prefix}/mall/goods/category`]: 'system:mall:goods:category:edit',
            [`${DELETE}${prefix}/mall/goods/category/*`]: 'system:mall:goods:category:delete',
            [`${POST}${prefix}/mall/goods/category/tag`]: 'system:mall:goods:category:tag:add',
            [`${PUT}${prefix}/mall/goods/category/tag`]: 'system:mall:goods:category:tag:edit',
            [`${DELETE}${prefix}/mall/goods/category/tag/*`]: 'system:mall:goods:category:tag:delete',

            [`${GET}${prefix}/mall/integral/list`]: 'system:mall:integral:list',
            [`${GET}${prefix}/mall/integral/:id`]: 'system:mall:integral:detail',
            [`${POST}${prefix}/mall/integral`]: 'system:mall:integral:add',
            [`${PUT}${prefix}/mall/integral`]: 'system:mall:integral:edit',
            [`${DELETE}${prefix}/mall/integral/*`]: 'system:mall:integral:delete',

            [`${GET}${prefix}/mall/goods/list`]: 'system:mall:goods:list',
            [`${GET}${prefix}/mall/goods/:id`]: 'system:mall:goods:detail',
            [`${POST}${prefix}/mall/goods`]: 'system:mall:goods:add',
            [`${PUT}${prefix}/mall/goods`]: 'system:mall:goods:edit',
            [`${DELETE}${prefix}/mall/goods/*`]: 'system:mall:goods:delete',

            [`${GET}${prefix}/banner/list`]: 'system:banner:list',
            [`${GET}${prefix}/banner/:id`]: 'system:banner:detail',
            [`${POST}${prefix}/banner`]: 'system:banner:add',
            [`${PUT}${prefix}/banner`]: 'system:banner:edit',
            [`${DELETE}${prefix}/banner/*`]: 'system:banner:delete',

            [`${GET}${prefix}/shop/goods/list`]: 'system:shop:goods:list',
            [`${GET}${prefix}/shop/goods/*`]: 'system:shop:goods:detail',
            [`${POST}${prefix}/shop/goods/audit`]: 'system:shop:goods:audit',

            [`${GET}${prefix}/order/list`]: 'system:order:list',
            [`${GET}${prefix}/order/after/sale/list`]: 'system:order:after:sale:list',
            [`${GET}${prefix}/order/comment/list`]: 'system:order:comment:list',
            [`${DELETE}${prefix}/order/comment/*`]: 'system:order:comment:delete',
            [`${GET}${prefix}/order/ticket/list`]: 'system:order:ticket:list',

            [`${GET}${prefix}/user/list`]: 'system:user:list',
            [`${GET}${prefix}/user/*`]: 'system:user:detail',
            [`${POST}${prefix}/user/audit/switch`]: 'system:user:audit:switch',

            [`${GET}${prefix}/user/auth/audit/list`]: 'system:user:auth:audit:list',
            [`${POST}${prefix}/user/auth/audit`]: 'system:user:audit:audit',

            [`${GET}${prefix}/article/list`]: 'system:article:list',
            [`${GET}${prefix}/article/*`]: 'system:article:detail',
            [`${POST}${prefix}/article`]: 'system:article:add',
            [`${PUT}${prefix}/article`]: 'system:article:edit',
            [`${DELETE}${prefix}/article/*`]: 'system:article:delete',

            [`${GET}${prefix}/wiki/list`]: 'system:wiki:list',
            [`${GET}${prefix}/wiki/*`]: 'system:wiki:detail',
            [`${POST}${prefix}/wiki`]: 'system:wiki:add',
            [`${PUT}${prefix}/wiki`]: 'system:wiki:edit',
            [`${DELETE}${prefix}/wiki/*`]: 'system:wiki:delete',

            [`${GET}${prefix}/rule/helper/list`]: 'system:rule:helper:list',
            [`${GET}${prefix}/rule/helper/*`]: 'system:rule:helper:detail',
            [`${POST}${prefix}/rule/helper`]: 'system:rule:helper:add',
            [`${PUT}${prefix}/rule/helper`]: 'system:rule:helper:edit',
            [`${DELETE}${prefix}/rule/helper/*`]: 'system:rule:helper:delete',

            [`${GET}${prefix}/rule/agreement/list`]: 'system:rule:agreement:list',
            [`${GET}${prefix}/rule/agreement/*`]: 'system:rule:agreement:detail',
            [`${POST}${prefix}/rule/agreement`]: 'system:rule:agreement:add',
            [`${PUT}${prefix}/rule/agreement`]: 'system:rule:agreement:edit',
            [`${DELETE}${prefix}/rule/agreement/*`]: 'system:rule:agreement:delete',

            [`${GET}${prefix}/sys/param`]: 'system:sysparam:query',
            [`${POST}${prefix}/sys/param`]: 'system:sysparam:save',

            [`${GET}${prefix}/withdraw/list`]: 'system:withdraw:list',
            [`${POST}${prefix}/withdraw/audit`]: 'system:withdraw:audit',


        };

        /**
         *
         * @type {boolean}
         */


            //验证权限
        const method = ctx.method.toUpperCase();

        let loginUserInfo = ctx.getLoginInfo();
        let isAdmin = loginUserInfo.isAdmin;
        let operatorId = loginUserInfo.operatorId;

        ctx.app.logger.info(`loginUserInfo:`, loginUserInfo);
        ctx.app.logger.info(`isAdmin:`, isAdmin, typeof isAdmin);

        if (isAdmin !== true && !noPermissionList.includes(method + '-' + ctx.path)) {


            let perms = await ctx.service.sysmenu.selectMenuPermsByUserId(operatorId);

            let permsMap = {};
            perms.map(function (item) {
                permsMap[item] = true;
            });

            let rPath = method + '-' + ctx.path;

            let rPerm = null;
            for (let key in permissionMap) {
                let re = globToRegExp(key);
                let ttre = re.test(rPath);
                if (ttre === true) {
                    rPerm = permissionMap[key];
                    break;
                }
            }
            console.info('isAdmin========', rPerm, rPath, isAdmin);
            ctx.app.logger.info('isAdmin========', rPerm, rPath, isAdmin);
            if (rPerm && !permsMap[rPerm]) {
                console.error(`请求访问：${ctx.path}，无权访问，无法访问系统资源`);
                ctx.app.logger.error(`请求访问：${ctx.path}，无权访问，无法访问系统资源`);
                return ctx.body = ResponseUtil.resError(VRCE.ERROR403[1], VRCE.ERROR403[0])
            }
        }

        await next();
    }
};
